package com.example.amoxicilin.null_test.main;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.amoxicilin.null_test.R;
import com.example.amoxicilin.null_test.domain.Answer;
import com.example.amoxicilin.null_test.domain.Status;
import com.example.amoxicilin.null_test.domain.User;
import com.example.amoxicilin.null_test.tools.HttpUtils;
import com.example.amoxicilin.null_test.tools.JsonTools;

import java.io.InputStream;
import java.net.URL;
import java.util.List;

import static com.example.amoxicilin.null_test.main.MeFragment.SHOW_RESPONSE;

public class QuestionActivity extends AppCompatActivity {


    private TextView tv;
    private int Questionnumber;
    private int usernumber;
    private RecyclerView recyclerView;
    private FloatingActionButton floatingActionButton;

    private String html;
    List<String> answerlist;

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg){
            super.handleMessage(msg);
            String response=(String) msg.obj;
            answerlist = JsonTools.jsonSplit(response);
            recyclerView.setAdapter(new RecyclerView.Adapter()
            {
                class MyViewHolder extends RecyclerView.ViewHolder{

                    public final ImageView head;
                    public final TextView content;
                    public final TextView person;
                    public final ImageView upvote;
                    public final TextView upvotenumber;
                    public MyViewHolder(View itemView) {
                        super(itemView);
                        head=(ImageView)itemView.findViewById(R.id.answer_person_img);
                        content=(TextView)itemView.findViewById(R.id.answer_content);
                        person=(TextView)itemView.findViewById(R.id.answer_person);
                        upvote=(ImageView) itemView.findViewById(R.id.answer_upvote);
                        upvotenumber=(TextView)itemView.findViewById(R.id.answer_upvotenumber);


                    }
                }


                @Override
                public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                    View view = LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.item_answer, parent, false);
                    return new MyViewHolder(view);
                }

                @Override
                public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
                    final MyViewHolder myViewHolder=(MyViewHolder) holder;
                    final Answer answer = JsonTools.getAnswer("answer", answerlist.get(position));//获取答案对象
                    int usernumber;
                    usernumber=answer.getPerson();
                    String path2 = "http://120.24.94.172/JsonTestServer/jsonTest?type1=query&type2=user&key=number&content="+usernumber;//每个回答对应的人
                    String jsonString2 = HttpUtils.getJsonContent(path2);
                    User user=JsonTools.getUser(null,jsonString2);
                    myViewHolder.person.setText(user.getName());
                    myViewHolder.content.setText(answer.getContent());
                    myViewHolder.upvotenumber.setText(""+answer.getUpvote());
                    myViewHolder.content.setEllipsize(TextUtils.TruncateAt.valueOf("END"));
                    Log.i("nimasile", "onBindViewHolder: "+user.getName()+answer.getContent());


                    myViewHolder.upvote.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String path="http://120.24.94.172/JsonTestServer/jsonTest?" +
                                    "type1=modify&type2=answerupvote&anumber="+answer.getAnumber();
                            Log.i("nima", "onClick: "+path);
                            String UpvoteString=HttpUtils.getJsonContent(path);
                            Status status=JsonTools.getStatus(null,UpvoteString);
                            if(status.getStatusId()==1)
                            {
                                myViewHolder.upvotenumber.setText(""+(answer.getUpvote()+1));
                                Toast.makeText(QuestionActivity.this,"点赞+1 ！",Toast.LENGTH_SHORT).show();
                            }
                        }
                    });


                    holder.itemView.setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(View v) {
                            Intent intent =new Intent();
                            intent.setClass(QuestionActivity.this,AnswerActivity.class);
                            Bundle bundle=new Bundle();
                            bundle.putString("AnswerContent",answer.getContent());
                            bundle.putInt("QuestionNumber",Questionnumber);
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    });


                }

                @Override
                public int getItemCount() {
                    return answerlist.size();
                }
            });
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);
        Toolbar toolbar = (Toolbar) findViewById(R.id.question_toolbar);
        setSupportActionBar(toolbar);

        Bundle bundle=getIntent().getExtras();
        String text=bundle.getString("Content");
        Questionnumber=bundle.getInt("QuestionNumber");
        usernumber=bundle.getInt("usernumber");

        html = text;
        Spanned sp = Html.fromHtml(html, new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(String source) {
                InputStream is = null;
                try {
                    is = (InputStream) new URL(source).getContent();
                    Drawable d = Drawable.createFromStream(is, "src");
                    d.setBounds(0, 0, d.getIntrinsicWidth(),
                            d.getIntrinsicHeight());
                    is.close();
                    return d;
                } catch (Exception e) {
                    return null;
                }
            }
        }, null);

        tv=(TextView)findViewById(R.id.question_text);
        tv.setText(sp);
        floatingActionButton=(FloatingActionButton)findViewById(R.id.study_fab);
        recyclerView=(RecyclerView)findViewById(R.id.answer_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        initData();

        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(QuestionActivity.this,AnswerSubmitActivity.class);
                Bundle bundle =new Bundle();
                bundle.putInt("Answernumber",answerlist.size());
                bundle.putInt("QuestionNumber",Questionnumber);
                bundle.putInt("usernumber",usernumber);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void initData(){

        String path = "http://120.24.94.172/JsonTestServer/jsonTest?type1=query&type2=answer&key=q_number&content="+Questionnumber;
        String jsonString = HttpUtils.getJsonContent(path);
        Message message= new Message();
        message.what=SHOW_RESPONSE;
        message.obj=jsonString;
        handler.sendMessage(message);
    }
}

